package nmklthk.rss.reader;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class FeedsList extends ListActivity {

	private static final int ACTIVITY_INSERT = 2;
	private static final int ACTIVITY_ABOUT = 4;
	private static final int ACTIVITY_VIEW = 3;
	
	private NewsDroidDB droidDB;
	private List<Feed> feeds;
	
	@Override
	protected void onCreate(Bundle icicle) {
		try {
	        super.onCreate(icicle);
	        setContentView(R.layout.feeds_list);
	        droidDB = new NewsDroidDB(this);
	        droidDB.open();
	        fillData();
	        showWelcomeScreen();
	        
		} catch (Throwable e) {
			Log.e("NewsDroid",e.toString());
		}
	}
	
	private void showWelcomeScreen()
	{
		LayoutInflater inflater = getLayoutInflater();
		View layout = inflater.inflate(R.layout.toast_layout,
		                               (ViewGroup) findViewById(R.id.toast_layout_root));

		ImageView image = (ImageView) layout.findViewById(R.id.wcimage);
		image.setImageResource(R.drawable.icon);
		TextView text = (TextView) layout.findViewById(R.id.wctext);
		text.setText("EZ RSS Reader v1.0");

		Toast toast = new Toast(getApplicationContext());
		toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
		toast.setDuration(Toast.LENGTH_LONG);
		toast.setView(layout);
		toast.show();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, ACTIVITY_INSERT, 0 , R.string.menu_insert);
        menu.add(0, ACTIVITY_ABOUT , 0 , R.string.menu_info);
        return true;
    }
	
	@Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        super.onMenuItemSelected(featureId, item);
        switch(item.getItemId()) {
        case ACTIVITY_INSERT:
            createFeed();
            break;
        case ACTIVITY_ABOUT:
        	Context context = getApplicationContext();
        	CharSequence text = "About us \n\nEZ RSS Reader \nCS426 Project by 0712711 - 0712736";
        	int duration = Toast.LENGTH_SHORT;

        	Toast toast = Toast.makeText(context, text, duration);
        	toast.setGravity(Gravity.BOTTOM, 0, 0);
        	toast.show();
        	break;
        }
        
        return true;
    }
	
	private void createFeed() {
		Intent i = new Intent(this, URLEditor.class);
		startActivityForResult(i, ACTIVITY_INSERT);
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		fillData();
	}

	@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        Intent i = new Intent(this, ArticlesList.class);
        i.putExtra("feed_id", feeds.get(position).feedId);
        i.putExtra("title", feeds.get(position).title);
        i.putExtra("url", feeds.get(position).url.toString());        
        startActivityForResult(i, ACTIVITY_VIEW);
    }
	
	private void fillData() {

        List<String> items = new ArrayList<String>();

        feeds = droidDB.getFeeds();
        for (Feed feed : feeds) {
            items.add(feed.title);
        }
        
        ArrayAdapter<String> notes = 
            new ArrayAdapter<String>(this, R.layout.feeds_row, items);
        setListAdapter(notes);
	}

	
	
}
